package com.wwwarehouse.common.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.activity.tempcardlist.TempCardListFragment;
import com.wwwarehouse.common.bean.PushReceiverLoginBean;
import com.wwwarehouse.common.bean.response.AuthOnlineAnswerResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.CompleteImportPushBean;
import com.wwwarehouse.common.bean.response.PCLoginConfirmResponseBean;
import com.wwwarehouse.common.bean.response.PushResultClass;
import com.wwwarehouse.common.bean.response.ScanDataResponseBean;
import com.wwwarehouse.common.bean.response.TCScanQrCodeBean;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.eventbus_event.AgainLoginEvent;
import com.wwwarehouse.common.eventbus_event.AppLoginEvent;
import com.wwwarehouse.common.eventbus_event.AuthIsOnlineEvent;
import com.wwwarehouse.common.eventbus_event.GetLoginLinkEvent;
import com.wwwarehouse.common.eventbus_event.LogoutEvent;
import com.wwwarehouse.common.eventbus_event.MessageCenterOpenedEvent;
import com.wwwarehouse.common.eventbus_event.NotificationEvent;
import com.wwwarehouse.common.eventbus_event.PauseLoginEvent;
import com.wwwarehouse.common.eventbus_event.ScanResultEvent;
import com.wwwarehouse.common.fragment.CommonBridgeWebViewFragment;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.service.BluetoothService;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.PermissionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.common.constant.Constant.KEY_SCAN_AUTHORITY;
import static com.wwwarehouse.common.constant.Constant.KEY_SECURITY_QR_CODE;
import static com.wwwarehouse.common.constant.Constant.URL_SCAN_DATA;

public class BaseCardDeskActivity extends BaseActivity implements PermissionUtils.PermissionCallbacks {
    protected DrawerLayout mDrawerLayout;
    private ImageView mScanbt;                      // 扫一扫
    private ImageView mScanbtFg;                    // 每个fragment上面的扫一扫
    protected ImageView mFgBackBt = null;           // fragment上的返回按钮
    private ImageView mRefreshBt;                   // 刷新
    // 测试按钮
    private ImageView cardlistPageBtn;              //暂时的卡片页面按钮
    private OnButtonClick onButtonClick = null;     // 按钮点击事件

    private Stack<Fragment> backStack = null;       // Fragment栈

    // Fragment容器
    protected RelativeLayout mContainer = null;

    // title上四个操作按钮，由用户自己来定制其功能
    LinearLayout operBtLayout = null;
    Button titleBt1 = null;
    Button titleBt2 = null;
    Button titleBt3 = null;
    Button titleBt4 = null;

    RelativeLayout mFragmentTitleLayout = null;     // fragment上的title layout

    TextView mFgTitleText = null;                   // fragment上的中间title

    LinearLayout mTitleSearchLayout = null;         // title上有筛选和搜索按钮
    ImageView mFilterBt = null;                     // 筛选按钮
    View mSearchImgV = null;                        // 搜索按钮
    ClearEditText mSearchEditV = null;              // 搜索页面搜索框
    LinearLayout mSearchInputLayout = null;         // 搜索输入的布局
    Button mSearchCancelBt = null;                  // 搜索页面取消按钮
    TextView mFunctionUnitText = null;              // 搜索那一层的业务单位按钮
    ImageView mSearchSortBt = null;                 // 搜索那一层的排序按钮

    long mExitTime = 0; // 退出时的时间差

    Handler mHandler = null;

    //扫描登录相关
    private static final int REQUEST_SCAN_DATA = 0;
    //三角链相关
    private static final int REQUEST_RECEIVE_MANAGER = 1;
    //权限相关
    private static final int REQUEST_AUTH_ONLINE_ANSWER = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_desk);
        init(savedInstanceState);
    }

    private void init(Bundle savedInstanceState) {
        mDrawerLayout = findView(R.id.dl_drawer);
        // 禁用卡片桌面DrawerLayout的手势滑动
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        cardlistPageBtn = findView(R.id.demo_page_tempcardlist);
        mScanbt = findView(R.id.scan_bt);
        mScanbtFg = findView(R.id.scan_bt2);
        mRefreshBt = findView(R.id.refresh_bt);
        mContainer = findView(R.id.container);
        operBtLayout = findView(R.id.oper_bt_layout);
        titleBt1 = findView(R.id.title_bt1);
        titleBt2 = findView(R.id.title_bt2);
        titleBt3 = findView(R.id.title_bt3);
        titleBt4 = findView(R.id.title_bt4);
        mFgBackBt = findView(R.id.fragment_back_bt);
        mFragmentTitleLayout = findView(R.id.fragment_title_layout);
        mFgTitleText = findView(R.id.fragment_title_text);

        //App Title上的几个按钮
        mTitleSearchLayout = findView(R.id.title_search_layout);
        mFilterBt = findView(R.id.shaixuan_img);                         //筛选
        mSearchImgV = findView(R.id.search_img);                         //搜索按钮
        mSearchEditV = findView(R.id.et_serach);                         //搜索输入框
        mSearchEditV.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mSearchInputLayout = findView(R.id.title_search_edit_layout);    //搜索输入布局
        mSearchCancelBt = findView(R.id.cancel_bt);                      //取消
        mFunctionUnitText = findView(R.id.function_unit_text);           //业务单位选择框
        mSearchSortBt = findView(R.id.search_sort_img);                  //排序

        onButtonClick = new OnButtonClick();

        cardlistPageBtn.setOnClickListener(onButtonClick);

        mScanbt.setOnClickListener(onButtonClick);
        mScanbtFg.setOnClickListener(onButtonClick);
        mRefreshBt.setOnClickListener(onButtonClick);

        titleBt1.setOnClickListener(onButtonClick);
        titleBt2.setOnClickListener(onButtonClick);
        titleBt3.setOnClickListener(onButtonClick);
        titleBt4.setOnClickListener(onButtonClick);

        mFilterBt.setOnClickListener(onButtonClick);
        mSearchImgV.setOnClickListener(onButtonClick);
        mSearchCancelBt.setOnClickListener(onButtonClick);
        mFunctionUnitText.setOnClickListener(onButtonClick);
        mSearchSortBt.setOnClickListener(onButtonClick);

        mSearchEditV.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击键盘上的搜索按钮，非实时搜索
                    hideSoftKeyBoard(mSearchEditV);

                    String searchText = mSearchEditV.getText().toString().trim();

                    Intent intent = new Intent("searchNoRealTimeClick");
                    intent.putExtra("searchNoRealTimeClick", searchText);
                    sendBroadcast(intent);

                    return true;
                }

                return false;
            }
        });

        mSearchEditV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //实时搜索
                Intent intent = new Intent("searchOnRealTimeClick");
                intent.putExtra("searchOnRealTimeClick", s.toString().trim());
                sendBroadcast(intent);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mHandler = new Handler();
        // 判断如果是uat或者是release就隐藏掉测试按钮
        if (LogUtils.isDebug()) {
            cardlistPageBtn.setVisibility(View.VISIBLE);
        } else {
            cardlistPageBtn.setVisibility(View.GONE);
        }
        backStack = new Stack<>();
        //注册EventBus
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (requestCode == 18345 && perms.size() == 1) {
            Intent it = new Intent(BaseCardDeskActivity.this, CaptureActivity.class);
            startActivityForResult(it, 11);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }

    protected class OnButtonClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == titleBt1) {
                Intent intent = new Intent("titleBt1");
                sendBroadcast(intent);
            } else if (v == titleBt2) {
                Intent intent = new Intent("titleBt2");
                sendBroadcast(intent);
            } else if (v == titleBt3) {
                Intent intent = new Intent("titleBt3");
                sendBroadcast(intent);
            } else if (v == titleBt4) {
                Intent intent = new Intent("titleBt4");
                sendBroadcast(intent);
            } else if (v == mScanbt || v == mScanbtFg) {
                PermissionUtils.requestPermissions(BaseCardDeskActivity.this, new String[]{Manifest.permission.CAMERA}, 18345);
            } else if (v == mRefreshBt) {
                Intent intent = new Intent("refreshClick");
                sendBroadcast(intent);
            } else if (v == cardlistPageBtn) {
                pushFragment(new TempCardListFragment());
            } else if (v == mFilterBt) {
                Intent intent = new Intent("filterClick");
                sendBroadcast(intent);
            } else if (v == mSearchImgV) {
                hideSearchTitle();
                showSearchInput();

                mSearchEditV.requestFocus();
                showSoftKeyBoard(mSearchEditV);
            } else if (v == mSearchCancelBt) {
                showSearchTitle();
                hideSearchInput();

                hideSoftKeyBoard(mSearchEditV);
                mSearchEditV.setText("");

                Intent intent = new Intent("cancelClick");
                sendBroadcast(intent);
            } else if (v == mFunctionUnitText) {
                Intent intent = new Intent("functionUnitClick");
                sendBroadcast(intent);
            } else if (v == mSearchSortBt) {
                Intent intent = new Intent("sortClick");
                sendBroadcast(intent);
            }
        }
    }

    // 切换任务，在任务、消息、群组、我的四个Fragment，这四个是根Fragment
    // 基于这四个Fragment往下走，用Fragment栈来切换
    Fragment mContent = null;

    // 入栈
    public void pushFragment(Fragment fg) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
//        trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//            trans.setCustomAnimations(R.anim.comm_slide_in_from_right, R.anim.comm_scale_out);
        trans.add(R.id.container, fg);
//        trans.replace(R.id.container, fg);
        trans.addToBackStack("mybackstack");
        trans.commitAllowingStateLoss();
        backStack.push(fg);
        //隐藏其他的title层
        recoveryTitleBtStatusAll();
        // 显示title
        mFragmentTitleLayout.setVisibility(View.VISIBLE);

        // 禁用手势滑动
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    // 不同module直接跳转fragment
    public void pushFragment(String target, Bundle bundle) {

        if (target == null || "".equals(target)) {
            if (LogUtils.isDebug()) {
                toast(getString(R.string.arouter_mistake));
            }
            return;
        }

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
        Fragment fg = (Fragment) ARouter.getInstance()
                .build(target)
                .with(bundle)
                .navigation();
        if (fg != null) {//不判断会崩
            trans.add(R.id.container, fg);
            trans.addToBackStack("mybackstack");
            trans.commitAllowingStateLoss();
            backStack.push(fg);
            //隐藏其他的title层
            recoveryTitleBtStatusAll();
            // 显示title
            mFragmentTitleLayout.setVisibility(View.VISIBLE);

            // 禁用手势滑动
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        } else {
            if (LogUtils.isDebug()) {
                toast(getString(R.string.arouter_notfound));
            }
        }
    }

    // 出栈
    public void popFragment() {
        //backStack为空的时候，不允许pop
        if (backStack.size() == 0) {
            return;
        }

        FragmentManager fm = getSupportFragmentManager();
        fm.popBackStackImmediate();
        Fragment fg = backStack.pop();
        FragmentTransaction trans = fm.beginTransaction();
        trans.remove(fg);
        trans.commitAllowingStateLoss();

        //隐藏其他的title层
        recoveryTitleBtStatusAll();

        //显示或者隐藏刷新按钮
        if (peekFragment() instanceof CommonBridgeWebViewFragment) {
            showRefreshBt();
            hideScanFgBt();
        } else {
            hideRefreshBt();
            showScanFgBt();
        }

        // 隐藏之前fragment显示出来的容错页面
        if (backStack.size() == 0) {
            mFragmentTitleLayout.setVisibility(View.GONE);
        }

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent it = new Intent("title");
                sendBroadcast(it);
            }
        }, 300);
        // pop的时候判断下面的fragment是否有侧滑
//        EventBus.getDefault().post(new MainEvent(""));
    }

    // 清空fragment栈
    public void cleanBackStack() {
        if (backStack == null) {
            return;
        }
        int size = backStack.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                popFragment();
            }
        }
    }

    // 清空fragment栈
    public void cleanBackStackWithLeftNumber(int leftNumber) {
        if (backStack == null) {
            return;
        }
        int size = backStack.size();
        if (size > 0) {
            for (int i = size - 1; i >= leftNumber; i--) {
                popFragment();
            }
        }
    }

    // 查看栈顶fragment
    public Fragment peekFragment() {
        if (backStack.size() > 0) {
            return backStack.peek();
        }

        return null;
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawers();
            return;
        }

        if (backStack.size() > 0) {
            popFragment();
            return;
        } else {
            // 如果没有的话就提示退出
            if (System.currentTimeMillis() - mExitTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
                return;
            }
            Intent it = new Intent(BaseCardDeskActivity.this, BluetoothService.class);
            stopService(it);
            finish();
        }
        super.onBackPressed();
    }

    // Token过期或未登录接收
    public void onEventMainThread(LogoutEvent event) {
        //删除token
        sp.deleteValue(this, Constant.sp_Token);

        String msg = event.getMsg();
        if ("logout".equals(msg)) {
            startActivity("/UserCenter/LoginActivity", null, false);

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Common.getInstance().finishAllActivity();
                }
            }, 800);
        }
    }

    // 二维码扫描接收
    public void onEventMainThread(ScanResultEvent event) {
        String scanData = event.getMsg();
        Map map = new HashMap();

        if (scanData.startsWith("http")) {
            scanData = scanData.substring(scanData.indexOf("?") + 1);
        }

        map.put("scanData", scanData);

        // 扫一扫
        NoHttpUtils.httpPost(URL_SCAN_DATA, map, mOnResponseListener, REQUEST_SCAN_DATA);
    }

    // PC端登录
    public void onEventMainThread(AppLoginEvent event) {
        String message = event.getMsg();
        PushReceiverLoginBean pushReceiverLoginBean = JSON.parseObject(message, PushReceiverLoginBean.class);

        Bundle bundle = new Bundle();
        bundle.putString(KEY_SECURITY_QR_CODE, pushReceiverLoginBean.getMsg());
        startActivity(Constant.PATH_PC_LOGIN_CONFIRM, bundle, false);
    }

    // 单点登录
    public void onEventMainThread(AgainLoginEvent event) {
        DialogTools.getInstance().showCustomWarning(this, null, getString(R.string.account_login_other_place), getString(R.string.i_know), true, new DialogTools.ConfirmListener() {
            @Override
            public void setConfirmListener(Dialog dialog, View view, String input) {
                dialog.dismiss();

                startActivity(Constant.PATH_LOGIN, null, false);

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Common.getInstance().finishAllActivity();
                    }
                }, 800);
            }
        });
    }

    // 通讯录被指定推送
    public void onEventMainThread(GetLoginLinkEvent event) {
        String message = event.getMsg();

        JSONObject jsonObject = JSON.parseObject(message);

        // 三角链-接收一级管理者/一级监控者 在线情况
        Map map = new HashMap();
        if (jsonObject != null)
            map.put("creatorUserId", jsonObject.getString("creatorUserId"));
        NoHttpUtils.httpPost(Constant.URL_RECEIVE_MANAGER, map, mOnResponseListener, REQUEST_RECEIVE_MANAGER);
    }


    // 响应在线消息(被授权人调用)
    public void onEventMainThread(AuthIsOnlineEvent event) {
        AuthOnlineAnswerResponseBean authOnlineAnswerResponseBean = JSON.parseObject(event.getMsg(), AuthOnlineAnswerResponseBean.class);

        Map map = new HashMap();
        map.put("processId", authOnlineAnswerResponseBean.getMsg().getProcessId());
        NoHttpUtils.httpPost(Constant.AUTH_ANSWER_ONLINE, map, mOnResponseListener, REQUEST_AUTH_ONLINE_ANSWER);
    }

    /**
     * 所有推送的点击事件处理
     *
     * @param event {msg: "xxx:12 , xxx:12 "} , type: "xxx"
     */
    public void onEventMainThread(NotificationEvent event) {
        PushResultClass pushResultClass = JSON.parseObject(event.getMsg(), PushResultClass.class);
        if (pushResultClass != null && pushResultClass.getType() != null && pushResultClass.getMsg() != null) {
            if ("PUSH_MESSAGE".equals(pushResultClass.getType())) {
                //打开系统推送的消息中心消息
                EventBus.getDefault().post(new MessageCenterOpenedEvent(event.getMsg()));
            } else if ("appLogin".equals(pushResultClass.getType())) {
                //PC端登录
                Bundle bundle = new Bundle();
                bundle.putString(KEY_SECURITY_QR_CODE, pushResultClass.getMsg().toString());
                startActivity(Constant.PATH_PC_LOGIN_CONFIRM, bundle, false);
            } else if ("completeImportTool".equals(pushResultClass.getType())) {
                //手机转PC，完成任务的推送消息，跳转到完成导入的页面
                CompleteImportPushBean completeImportPushBean = JSON.parseObject(pushResultClass.getMsg().toString(), CompleteImportPushBean.class);
                Bundle bundle = new Bundle();
                bundle.putLong(Constant.KEY_INTENT_IMPORT_TOOL_PUSH_CLICK, Long.valueOf(completeImportPushBean.getPushImportCount()));
                pushFragment(Constant.PATH_IMPORT_SUCCESS, bundle);
            } else if ("startImportTool".equals(pushResultClass.getType())) {
                //跳转到手机转PC的计时页面
                CompleteImportPushBean completeImportPushBean = JSON.parseObject(pushResultClass.getMsg().toString(), CompleteImportPushBean.class);
                Bundle bundle = new Bundle();
                bundle.putLong(Constant.KEY_INTENT_IMPORT_TIME, Long.valueOf(completeImportPushBean.getTimeCost()));
                pushFragment(Constant.PATH_DURATION_TIME, bundle);
            } else if ("authNotice".equals(pushResultClass.getType())) {
                //授权结果通知
//                    Bundle bundle = new Bundle();
//                    bundle.putString(Constant.KEY_AUTHORITY_NOTIFICATION, pushResultClass.getMsg().toString());
//                    pushFragment(Constant.PATH_AUTHORITY_NOTIFICATION, bundle);
            } else if ("pauseLogin".equals(pushResultClass.getType())) {
                //暂停登录
                EventBus.getDefault().post(new PauseLoginEvent(event.getMsg()));
            }
        }
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_SCAN_DATA:
                    if ("0".equals(commonClass.getCode())) {
                        ScanDataResponseBean scanDataResponseBean = JSON.parseObject(commonClass.getData().toString(), ScanDataResponseBean.class);
                        if ("LOGIN".equals(scanDataResponseBean.getScanType())) {
                            //扫码登录
                            PCLoginConfirmResponseBean pcLoginConfirmResponseBean = JSON.parseObject(commonClass.getData().toString(), PCLoginConfirmResponseBean.class);
                            Bundle bundle = new Bundle();
                            bundle.putString(KEY_SECURITY_QR_CODE, pcLoginConfirmResponseBean.getSecurityQrCode());
                            startActivity(Constant.PATH_PC_LOGIN_CONFIRM, bundle, false);
                        } else if ("CREATE_BUSINESS_MANAGER".equals(scanDataResponseBean.getScanType())) {
                            //三角链-指定管理者/监控者
                            TCScanQrCodeBean tcScanQrCodeBean = JSON.parseObject(commonClass.getData().toString(), TCScanQrCodeBean.class);
                            Bundle bundle = new Bundle();
                            bundle.putString(KEY_SECURITY_QR_CODE, tcScanQrCodeBean.getMessage());
                            bundle.putInt("type", Constant.TYPE_SCAN_CODE);
                            pushFragment(Constant.PATH_TC_SUCCESS, bundle);
                        } else if ("AUTH".equals(scanDataResponseBean.getScanType())) {
                            //扫码分配权限
//                            ScanAuthorityResponseBean scanAuthorityResponseBean = JSON.parseObject(commonClass.getData().toString(), ScanAuthorityResponseBean.class);
                            Bundle bundle = new Bundle();
                            bundle.putString(KEY_SCAN_AUTHORITY, commonClass.getData().toString());
                            pushFragment(Constant.PATH_AUTHORITY_RECEIVE, bundle);
                        }
                    } else if ("200025".equals(commonClass.getCode())) {
                        //200025 二维码不存在或已失效，请重新获取二维码
                        startActivity(Constant.PATH_PC_LOGIN_ERROR, null, false);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_RECEIVE_MANAGER:
                    if ("0".equals(commonClass.getCode())) {

                    }
                    break;
                case REQUEST_AUTH_ONLINE_ANSWER:
                    if ("0".equals(commonClass.getCode())) {
                        LogUtils.showInfoLog("响应推送成功");
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {

        }

        @Override
        public void onFinish(int what) {
        }
    };

    //--------------------------------------------下面是对外提供的一些接口的封装----------------------------------------

    // 下拉后显示四个自定义button
    boolean isOri_3 = true; // 是否是原始状态

    public void downPull_3() {
        if (isOri_3) {
            operBtLayout.setVisibility(View.VISIBLE);
        } else {
            operBtLayout.setVisibility(View.GONE);
        }

        isOri_3 = !isOri_3;
    }

    // 四个自定义按钮出来的时候，恢复原始状态， 隐藏转发按钮
    public void recoveryTitleBtStatus_3() {
        if (!isOri_3) {
            // 恢复过来
            operBtLayout.setVisibility(View.GONE);
            isOri_3 = true;
        }
    }

    // 下拉后显示带有筛选和搜索的title
    boolean isOri_4 = true; // 是否是原始状态

    public void downPull_4() {
        if (isOri_4) {
            mTitleSearchLayout.setVisibility(View.VISIBLE);
        } else {
            mTitleSearchLayout.setVisibility(View.GONE);
        }

        isOri_4 = !isOri_4;
    }

    // 筛选搜索title出来的时候，恢复原始状态
    public void recoveryTitleBtStatus_4() {
        if (!isOri_4) {
            // 恢复过来
            mTitleSearchLayout.setVisibility(View.GONE);
            showFilterBt();
            showSortBt();
            isOri_4 = true;
        }
    }

    //直接恢复所有的原始状态
    public void recoveryTitleBtStatusAll() {
        hideSoftKeyBoard(mSearchEditV);
        hideSearchInput();
        hideFunctionUnitText();
        hideRefreshBt();
        showScanFgBt();
        recoveryTitleBtStatus_3();
        recoveryTitleBtStatus_4();
    }

    // ---------------------title上四个自定义操作按钮--------------------------
    public void setTitleBt1Text(String text) {
        titleBt1.setText(text);
    }

    public void setTitleBt1Text(int id) {
        titleBt1.setText(id);
    }

    public void setTitleBt1Enable(boolean enable) {
        titleBt1.setEnabled(enable);
    }

    public void setTitleBt2Text(String text) {
        titleBt2.setText(text);
    }

    public void setTitleBt2Text(int id) {
        titleBt2.setText(id);
    }

    public void setTitleBt2Enable(boolean enable) {
        titleBt2.setEnabled(enable);
    }

    public void setTitleBt3Text(String text) {
        titleBt3.setText(text);
    }

    public void setTitleBt3Text(int id) {
        titleBt3.setText(id);
    }

    public void setTitleBt3Enable(boolean enable) {
        titleBt3.setEnabled(enable);
    }

    public void setTitleBt4Text(String text) {
        titleBt4.setText(text);
    }

    public void setTitleBt4Text(int id) {
        titleBt4.setText(id);
    }

    public void setTitleBt4Enable(boolean enable) {
        titleBt4.setEnabled(enable);
    }

    // 显示第一个按钮
    public void showTitleBt1() {
        operBtLayout.setVisibility(View.VISIBLE);
        titleBt1.setVisibility(View.VISIBLE);
        titleBt2.setVisibility(View.GONE);
        titleBt3.setVisibility(View.GONE);
        titleBt4.setVisibility(View.GONE);
    }

    // 显示前两个按钮
    public void showTitleBt2() {
        operBtLayout.setVisibility(View.VISIBLE);
        titleBt1.setVisibility(View.VISIBLE);
        titleBt2.setVisibility(View.VISIBLE);
        titleBt3.setVisibility(View.GONE);
        titleBt4.setVisibility(View.GONE);
    }

    // 显示前三个按钮
    public void showTitleBt3() {
        operBtLayout.setVisibility(View.VISIBLE);
        titleBt3.setVisibility(View.VISIBLE);
        titleBt2.setVisibility(View.VISIBLE);
        titleBt1.setVisibility(View.VISIBLE);
        titleBt4.setVisibility(View.GONE);
    }

    // 四个按钮都显示
    public void showTitleBt4() {
        operBtLayout.setVisibility(View.VISIBLE);
        titleBt4.setVisibility(View.VISIBLE);
        titleBt2.setVisibility(View.VISIBLE);
        titleBt3.setVisibility(View.VISIBLE);
        titleBt1.setVisibility(View.VISIBLE);
    }

    public void hideTitleBt() {
        isOri_3 = true;
        operBtLayout.setVisibility(View.GONE);
        titleBt4.setVisibility(View.GONE);
        titleBt2.setVisibility(View.GONE);
        titleBt3.setVisibility(View.GONE);
        titleBt1.setVisibility(View.GONE);
    }

    // 禁用卡片桌面DrawerLayout的手势滑动
    public void disableDrawerLayout() {
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    // 打开DrawerLayout
    public void showDrawerLayout() {
        mDrawerLayout.openDrawer(Gravity.RIGHT);
    }

    // 关闭DrawerLayout
    public void hideDrawerLayout() {
        mDrawerLayout.closeDrawers();
    }

    // 设置抽屉布局并开启手势滑动
    public void setDrawerLayout(Fragment fragment) {
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.dl_menu, fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    // 隐藏Fragment上面的返回按钮
    public void hideBackBt() {
        mFgBackBt.setVisibility(View.GONE);
    }

    // 显示Fragment上面的返回按钮
    public void showBackBt() {
        mFgBackBt.setVisibility(View.VISIBLE);
    }

    // 显示Fragment上面的扫一扫按钮
    public void showScanFgBt() {
        mScanbtFg.setVisibility(View.VISIBLE);
    }

    // 隐藏Fragment上面的扫一扫按钮
    public void hideScanFgBt() {
        mScanbtFg.setVisibility(View.GONE);
    }

    //------------------------------获取title上有筛选按钮和搜索按钮这一层的控件-------------------
    // 显示刷新按钮  Webview界面用
    public void showRefreshBt() {
        mRefreshBt.setVisibility(View.VISIBLE);
    }

    // 隐藏刷新按钮
    public void hideRefreshBt() {
        mRefreshBt.setVisibility(View.GONE);
    }

    // 显示筛选按钮
    public void showFilterBt() {
        mFilterBt.setVisibility(View.VISIBLE);
    }

    // 隐藏筛选按钮
    public void hideFilterBt() {
        mFilterBt.setVisibility(View.GONE);
    }

    // 显示带有筛选按钮的搜索控件
    public void showSearchTitle() {
        mTitleSearchLayout.setVisibility(View.VISIBLE);
    }

    // 隐藏搜索图标
    public void hideSearchImgV() {
        mSearchImgV.setVisibility(View.GONE);
    }

    // 显示搜索图标
    public void showSearchImgV() {
        mSearchImgV.setVisibility(View.VISIBLE);
    }

    // 隐藏带有筛选按钮的搜索控件
    public void hideSearchTitle() {
        mTitleSearchLayout.setVisibility(View.GONE);
    }

    // 显示带有取消按钮的搜索控件
    public void showSearchInput() {
        mSearchInputLayout.setVisibility(View.VISIBLE);
    }

    // 隐藏带有取消按钮的搜索控件
    public void hideSearchInput() {
        mSearchInputLayout.setVisibility(View.GONE);
    }

    // 显示取消按钮
    public void showCancelBt() {
        mSearchCancelBt.setVisibility(View.VISIBLE);
    }

    // 隐藏取消按钮
    public void hideCancelBt() {
        mSearchCancelBt.setVisibility(View.GONE);
    }

    // 显示搜索页面的业务单位按钮
    public void showFunctionUnitText() {
        mFunctionUnitText.setVisibility(View.VISIBLE);
    }

    // 隐藏搜索页面的业务单位按钮
    public void hideFunctionUnitText() {
        mFunctionUnitText.setVisibility(View.GONE);
    }

    // 设置业务单位按钮的文本
    public void setFunctionUnitText(String text) {
        mFunctionUnitText.setText(text);
    }

    public void setFunctionUnitText(int id) {
        mFunctionUnitText.setText(id);
    }

    // 获取业务单位按钮的文本
    public String getFunctionUnitText() {
        return mFunctionUnitText.getText().toString().trim();
    }

    // 获取业务单位的TextView
    public TextView getFunctionUnitTextView() {
        return mFunctionUnitText;
    }

    // 显示排序按钮
    public void showSortBt() {
        mSearchSortBt.setVisibility(View.VISIBLE);
    }

    // 隐藏排序按钮
    public void hideSortBt() {
        mSearchSortBt.setVisibility(View.GONE);
    }

    // 获取排序按钮的ImageView
    public ImageView getSortImageView() {
        return mSearchSortBt;
    }

    // 显示设置fragment上面的title
    public void setTitle(CharSequence text) {
        mFgTitleText.setText(text);
    }

    public void setTitle(int resource) {
        mFgTitleText.setText(resource);
    }
    //----------------------------end---------------------------------------
}
